<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="渠道名称" prop="channel">
            <el-select
              v-model="queryParams.channel"
              placeholder="渠道名称"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in channelList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="loading" :data="list">
          <el-table-column label="注册数" align="center" prop="registerNum"/>
          <el-table-column label="注册转化率" align="center" prop="pv"/>
          <el-table-column label="完成认证数" align="center" prop="statuSuccessNum"/>
          <el-table-column label="完成认证率" align="center" prop="statuSuccessNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.statuSuccessNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次申请数" align="center" prop="applyNum"/>
          <el-table-column label="首次认证率" align="center" prop="applyNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.applyNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次放款数" align="center" prop="payNum"/>
          <el-table-column label="首次放款通过率" align="center" prop="payNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.payNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次放款金额" align="center" prop="totalMoney"/>
        </el-table>

        <el-table border v-loading="loading" :data="indexList" style="margin-top: 20px;">
          <el-table-column type="index" width="50"/>
          <el-table-column label="日期" align="center" prop="buildDate"/>
          <el-table-column label="渠道码" align="center" prop="channel">
            <template slot-scope ="scope">
              <span v-if="scope.row.channel">{{scope.row.channel}}</span>
              <span v-else>全部</span>
            </template>
          </el-table-column>
          <el-table-column label="PV" align="center" prop="pv"/>
          <el-table-column label="UV" align="center" prop="uv"/>
          <el-table-column label="安装数" align="center" prop="pv"/>
          <el-table-column label="注册数" align="center" prop="registerNum"/>
          <el-table-column label="注册转化率" align="center" prop="pv"/>
          <el-table-column label="完成认证数" align="center" prop="statuSuccessNum"/>
          <el-table-column label="认证完成率" align="center" prop="statuSuccessNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.statuSuccessNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次申请" align="center" prop="applyNum"/>
          <el-table-column label="首次申请率" align="center" prop="applyNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.applyNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次放款" align="center" prop="payNum"/>
          <el-table-column label="首次放款率" align="center" prop="payNum">
            <template slot-scope="scope">
              <span v-if="scope.row.registerNum == 0">0.00%</span>
              <span v-else>{{ scope.row.payNum / scope.row.registerNum | numFilter }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="首次放款金额" align="center" prop="totalMoney"/>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {listRegisterData, listTotalData} from "@/api/app/data/registerData";
import {channelList} from "@/api/app/channel";
export default {
  name: "RegisterData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      indexList: null,
      list: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      channelList: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginTime: undefined,
        endTime: undefined
      },
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getTotalList();
    this.getChannelList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = undefined;
        this.queryParams.endTime = undefined;
      }
      listRegisterData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.indexList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getTotalList() {
      if (this.dateRange) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = undefined;
        this.queryParams.endTime = undefined;
      }
      listTotalData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.data;
        }
      );
    },

    getChannelList(){
      channelList().then(response =>{
        this.channelList = response.data
      })
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        applyMoney: 0,
        manageMoney: 0,
        examineMoney: 0,
        serviceMoney: 0,
        cycle: 0,
        realMoney: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getTotalList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getIndexApply(id).then(response => {
        this.form = response.data;
        this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
        this.open = true;
        this.title = "修改";
      });
    },


    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateIndexApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIndexApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delIndexApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    handleMoney() {
      this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    }
  },
  filters: {
    numFilter: (value) => {
      return value = parseFloat(value * 100).toFixed(2)
    }
  }
};
</script>
